package com.whw.fy.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

public class LoadUtil {
    private static final Logger logger = LoggerFactory.getLogger(LoadUtil.class);

    public static String upload(String basePath, MultipartFile[] files, HttpServletRequest req) throws IOException {
        StringBuilder filesNames = new StringBuilder();
        if (files.length>0) {
            for (MultipartFile file : files) {
                String format= file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")+1);
                String fileName= UUID.randomUUID().toString()+"."+format;
//                String dir=basePath+"/";
//                File f=new File(basePath);
//                if (!f.exists()) {
//                    f.mkdirs();
//                }
                File saveFile = new File(basePath+fileName);
                file.transferTo(saveFile);
                filesNames.append(fileName);

                System.out.println("File saved path:"+basePath+fileName);
            }
            return filesNames.toString();
        }
        return null;
    }

}
